<?php
/**
 * Created by PhpStorm.
 * User: Mysic
 * Date: 2020/10/15
 * Time: 18:22
 */

namespace app\common\mysic\order;


use app\admin\model\BaseSetting;
use app\common\model\divide\System;
use app\common\model\Order;
use app\common\mysic\MysicBase;
use phpDocumentor\Reflection\DocBlock\Description;
use think\Exception;
use think\Model;

class SystemDividendOrderOp extends MysicBase implements OrderOpInterface
{
    /**
     * @var Model
     */
    protected $model;
    protected $agentsAmount;
    protected $order;
    protected $issue;
    protected $systemDividendOrderId;

    public function setModel(Model $model)
    {
        if (!($model instanceof System) || empty($model)) {
            throw new Exception('平台分账订单模型设置错误');
        }
        $this->model = $model;
        return $this;
    }

    public function getModel()
    {
        return $this->model;
    }

    public function setAgentsAmount($agentsAmount)
    {
        $this->agentsAmount = $agentsAmount;
        return $this;
    }

    public function setOrder(Model $order)
    {
        if (!($order instanceof Order) || empty($order)) {
            throw new Exception('用户订单模型模型设置错误');
        }
        $this->order = $order;
        return $this;
    }

    public function getOrder()
    {
        return $this->order;
    }

    public function setIssue(int $issue)
    {
        if (empty($issue) || $issue < 1) {
            throw new Exception('平台分账订单期号设置错误');
        }
        $this->issue = $issue;
        return $this;
    }

    public function getIssue()
    {
        return $this->issue;
    }

    public function create()
    {
        $systemConfig                = BaseSetting::getItem('wechat');
        $this->model->id = null;
        $this->model->account        = $systemConfig[ 'mch_id' ];//服务商商户id
        $this->model->order_no       = $this->order->order_no;//订单号
        $this->model->wxapp_id       = $this->order->wxapp_id;//微信小程序内部编号
        $this->model->abonus         = bcmul(strval($this->order->pay_price), '0.01', 2);//分账可得金额(单位/分)
        $this->model->amount         = bcsub($this->model->abonus, $this->agentsAmount,4);//分红金额(单位/分)
        $this->model->description    = '分账给系统服务商';
        $this->model->payment        = $this->order->pay_price;//实际支付总金额(单位/分)
        $this->model->transaction_id = $this->order->transaction_id;//微信内部订单
        $this->model->status         = 0;//结算状态 0待分账 1已分账 2分账失败
        $this->model->create_time    = time();//写入时间
        $this->model->periods_num    = $this->issue;//期号
        $this->model->isUpdate(false);
        $count = $this->model->save();
        $this->systemDividendOrderId = $this->model->getLastInsID();

        return $count > 0;
    }

    public function failure($msg)
    {
        $data = [
            'status'      => 2,
            'update_time' => time(),
            'fail_reason' => $msg
        ];
        $this->update($data);
    }

    public function success()
    {
        $data = [
            'status'      => 1,
            'update_time' => time(),
            'fail_reason' => '分账成功'
        ];
        $this->update($data);
    }

    protected function update($data)
    {
        $this->model->isUpdate(true)->save($data,['id' => $this->systemDividendOrderId]);
    }
}